package com.xqc.main.manage.config;

import com.xqc.main.manage.utils.RedisUtil;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Component
public class AuthInterceptor implements HandlerInterceptor {

  @Resource
  private RedisUtil redisUtil;

  /**
   * 方法执行前预处理
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//    //放行路径 TODO
//    String requestURI = request.getRequestURI();
//    //解析token
//    String token = request.getHeader("token");
//    if (Objects.isNull(token)) {
//      throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
//    }
//    String userId = JwtUtil.parseToken(token).getSubject();
//    if (Objects.isNull(userId)) {
//      throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
//    }
//    //缓存数据
//    UserVo userVo = (UserVo) redisUtil.get(RedisPrefixConstants.USER_INFO + userId);
//    if (Objects.isNull(userVo)) {
//      throw new CommonException(EmError.AUTH_TOKEN_EXPIRED);
//    }
//    // 是否被禁用
//    if (userVo.getWhetherBlock()) {
//      throw new CommonException(EmError.AUTH_USER_IS_BLOCK_LOGIN);
//    }
//    //存入UserContext
//    UserContext.setUser(userVo);
    return true;
  }

  /**
   * 方法执行后
   */
  @Override
  public void postHandle(HttpServletRequest request,
                         HttpServletResponse response,
                         Object handler,
                         @Nullable ModelAndView modelAndView) throws Exception {
  }

  /**
   * 页面渲染后
   */
  @Override
  public void afterCompletion(HttpServletRequest request,
                              HttpServletResponse response,
                              Object handler,
                              @Nullable Exception ex) throws Exception {
  }
}
